package com.yolib.sanyi;

import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.Html;
import android.text.Spanned;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.ZoomControls;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
import com.google.android.maps.OverlayItem;
import com.yolib.sanyi.tool.MapOverlay;

public class StoreMapActivity extends MapActivity {


	private String 		mAddX;
	private String 		mAddY; 

	private MapView 	mapview01;
	private Drawable 	mydrawable; 
	private MapController mapController;
	private ViewGroup 	mMapView;
	private View		mTitleView;
	private String		mtitleString;
	private TextView    mMapTitle;
	private ImageView	mBack;
	private Spanned 	mSpanned;
	
	MyLocationOverlay myLocationOverlay;
	
	@Override

	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);

		mMapView = (ViewGroup) ViewGroup.inflate(this, R.layout.activity_mapview, null);
		
		mMapTitle = (TextView)	mMapView.findViewById(R.id.map_view_name);
		mBack	  = (ImageView) mMapView.findViewById(R.id.BackImageView);
		
		
		if (this.isDebug()) {
			mapview01 = new MapView(this, "0_hkOYtoM3D7dHCv9L8xxrnrPU-SamG-jGcugNw"); // debug key
		}
		else {
			mapview01 = new MapView(this, "0WWnlzuscsvOmo__mDiub3zBuaH-VX4vN2gwcPQ"); // publish key
		}
		myLocationOverlay = new MyLocationOverlay(StoreMapActivity.this, mapview01);
		myLocationOverlay.enableMyLocation();
		
		RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
		
		params.addRule(RelativeLayout.BELOW, R.id.map_view_title);
		params.topMargin = -4;
		mMapView.addView(mapview01, params);
		
		mapview01.getOverlays().add(myLocationOverlay);
		mapview01.setBuiltInZoomControls(true);
		mapview01.setClickable(true);
		mapview01.setEnabled(true);

		setContentView(mMapView);

		mapController = mapview01.getController(); 
		mapController.setZoom(17);

		mydrawable = this.getResources().getDrawable(R.drawable.location);//指定標誌圖示

		MapOverlay mymapoverlay = new MapOverlay(mydrawable);

		mAddX	 = getIntent().getStringExtra("addX");
		mAddY	 = getIntent().getStringExtra("addY");

		double lon = Float.parseFloat(mAddX)*1E6;
		double lat = Float.parseFloat(mAddY)*1E6;
		
		if(getIntent().getStringExtra("title") != null) {
			mtitleString = getIntent().getStringExtra("title");
			mSpanned = Html.fromHtml(mtitleString);
			mMapTitle.setText(mSpanned);
		}

		//加入經緯度

		GeoPoint addPoint = new GeoPoint((int)lat, (int)lon);  //指定位置
		OverlayItem myoverlayitem = new OverlayItem(addPoint, mSpanned.toString(), "");
		mymapoverlay.addOverlayItem(myoverlayitem);
		mapview01.getOverlays().add(mymapoverlay);
		mapview01.getController().setCenter(addPoint);
		
		mTitleView = View.inflate(this, R.layout.view_map_title, null);
		MapView.LayoutParams geoLP = new MapView.LayoutParams(MapView.LayoutParams.WRAP_CONTENT, 
															  MapView.LayoutParams.WRAP_CONTENT, 
															  null,
															  0,
															  -100,
															  MapView.LayoutParams.BOTTOM_CENTER);
		geoLP.point = myoverlayitem.getPoint();
		mapview01.addView(mTitleView, geoLP);
		TextView title = (TextView)	findViewById(R.id.map_Title);
		title.setText(myoverlayitem.getTitle());
		
		mBack.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				finish();
				overridePendingTransition(R.anim.push_right_in, R.anim.push_right_out);				
			}
		});
	}



	@Override

	protected boolean isRouteDisplayed() {

		return false;

	}

	private boolean isDebug() {
		PackageManager mgr = this.getPackageManager();
		try {
			ApplicationInfo info = mgr.getApplicationInfo(this.getPackageName(), 0);
			return (info.flags & ApplicationInfo.FLAG_DEBUGGABLE) == ApplicationInfo.FLAG_DEBUGGABLE;
		}
		catch (NameNotFoundException e) {
		}
		return false;
	}

}